package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Permission;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IPermissionService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("")
    @RequiresPermissions("permission:view")
    @PermissionName("权限管理")
    public String index(){
        return "permission";
    }

    @RequiresPermissions("permission:list")
    @PermissionName("权限列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return permissionService.list(qo);
    }

    @RequiresPermissions("permission:reload")
    @PermissionName("加载权限")
    @RequestMapping("reload")
    @ResponseBody
    public JSONResult reload(){
        JSONResult result = new JSONResult();
        try{
        permissionService.reload();
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("加载失败");
        }
        return result;
    }

    @RequestMapping("listAll")
    @ResponseBody
    public PageResult listAll(QueryObject qo){
        return permissionService.listAll(qo);
    }



    @RequestMapping("queryForRoleId")
    @ResponseBody
    public PageResult queryForRoleId(Long roleId){
        List<Permission> permissionList = permissionService.queryForRoleId(roleId);
        return new PageResult(permissionList.size(),permissionList);
    }
}
